package com.xzq.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 实现在框架中使用	session等servlet原生对象的另一种方式
 *
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,SessionAware,RequestAware,ApplicationAware,ServletContextAware{
	//protected 让子类可以访问
	//自定义一个session属性	由于实现了SessionAware接口
	//struts2会自动地调用setSession()方法 把原生的servlet session传值
	protected Map<String, Object> session;
	protected Map<String, Object> application;
	protected Map<String, Object> request;
	protected ServletContext context;
	protected ServletRequest sere;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
		
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
		
	}
	
	public String getRealPath(String path){
		return context.getRealPath(path);//转换成web应用所在的系统路径
	}

	@Override
	public void setServletRequest(HttpServletRequest sere) {
		this.sere = sere;
		
	}
	
	 /**
     * 将对象转换成JSON字符串，并响应回前台()
     * 
     * @param object
     * @throws IOException
     */ 
    public void writeJson(Object object) { 
    	try { 
    		String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm"); 
    		ServletActionContext.getRequest().getSession(); 
    		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 
    		ServletActionContext.getResponse().getWriter().write(json); 
    		ServletActionContext.getResponse().getWriter().flush();
    		ServletActionContext.getResponse().getWriter().close(); 
    		} catch (IOException e) {
    			e.printStackTrace(); 
    		} 
    } 
     /** 
     * @param object
     */ 
    public void writeJsonOut(Object object){ 
    	OutputStream outputStream=null; 
    	String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm"); 
    	ServletActionContext.getRequest().getSession(); 
    	ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 
    	HttpServletResponse response = ServletActionContext.getResponse(); byte[] jsonby; 
    	try { 
    		jsonby = json.toString().getBytes("utf-8"); 
    		response.setContentLength(jsonby.length); 
    		outputStream=response.getOutputStream(); 
    		outputStream.write(jsonby); outputStream.flush(); 
    		outputStream.close(); 
    	} catch (Exception e) {
    		// TODO Auto-generated catch block 
    		e.printStackTrace();
    	}
    }
    
    /**
     * 参数是已经封装好的json集合
     * @param map
     */ 
    public void writeJson(Map<String, Object>map) { 
    	OutputStream outputStream=null; 
    	try { 
    		HttpServletRequest request = ServletActionContext.getRequest(); 
    		HttpServletResponse response = ServletActionContext.getResponse(); 
    		request.setCharacterEncoding("UTF-8"); 
    		response.setContentType("text/json;charset=utf-8");
    		request.getSession(); 
    		byte[] jsonby=map.toString().getBytes("utf-8"); 
    		response.setContentLength(jsonby.length); 
    		outputStream=response.getOutputStream(); 
    		outputStream.write(jsonby); 
    		outputStream.flush(); 
    		outputStream.close(); 
    	} catch (Exception e) { 
    		e.printStackTrace(); 
    	}finally{ 
    		if (outputStream!=null) { 
    			try { 
    				outputStream.close(); 
	    		} catch (Exception e) {
	    			e.printStackTrace(); 
	    		} 
    		}
    	}

    }
}
